package com.woniuxy.educationplatform.mapper;

import com.woniuxy.educationplatform.bean.GoodsBean;
import com.woniuxy.educationplatform.bean.ProjectBean;
import com.woniuxy.educationplatform.bean.TeacherBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface GoodsMapper {


    /**
     * 通过Id查询商品
     *
     * @param id
     * @return
     */
    @Results({
            @Result(property = "teacherBean", column = "fk_teacher_id", javaType = TeacherBean.class, one =
            @One(select = "com.woniuxy.educationplatform.mapper.TeacherMapper.findById", fetchType = FetchType.LAZY)),
            @Result(property = "projectBean", column = "fk_project_id", javaType = ProjectBean.class, one =
            @One(select = "com.woniuxy.educationplatform.mapper.ProjectMapper.findById", fetchType = FetchType.LAZY))
    })
    @Select("select * from goods_info where id = #{id} and del=0")
    GoodsBean findById(int id);

    /**
     * 查询所有的商品
     *
     * @return
     */
    @Select("select * from goods_info where del=0 and brand_label=0 ")
    List<GoodsBean> findAll();

    /**
     * 根据Id删除商品
     * 弱删除 修改状态
     *
     * @param id
     * @return
     */
    @Update("update  goods_info set del=1 where id=#{id} ")
    int deleteById(int id);

    /**
     * 查询销量前五
     *
     * @return
     */
    @Select("select  goods_name as goodsName,image,sales from goods_info where del=0 order by sales desc limit 0,5 ")
    List<GoodsBean> findGoodsSoldTop5();


    /**
     * 查询访问量前五
     *
     * @return
     */
    @Select("select goods_name as goodsName,image,visits from goods_info where del=0 order by visits desc limit 0,5")
    List<GoodsBean> findGoodsVisitedTop5();


    /**
     * 新增非实物商品
     *
     * @param goodsBean
     * @return 受影响的行数
     */
    @Options(
            useGeneratedKeys = true,
            keyProperty = "g.id"
    )
    @Insert("insert into goods_info " +
            "(goods_name,goods_desc,brand_label,class_hour,image,fk_teacher_id,fk_project_id," +
            "useful,detail,market_price,sales_price,stock,upload_time,status,goods_no,visits,del,sales)" +
            "values(#{g.goodsName},#{g.goodsDesc},#{g.brandLabel},#{g.classHour},#{g.image},#{g.fkTeacherId},#{g.fkProjectId}," +
            "#{g.useful},#{g.detail},#{g.marketPrice},#{g.salesPrice},#{g.stock},now(),#{g.status},#{g.goodsNo},#{g.visits},'0',#{g.sales})")
    int saveGoodsBean(@Param("g") GoodsBean goodsBean);


    /**
     * 创建实物商品
     *
     * @param goodsBean
     * @return
     */
    @Options(
            useGeneratedKeys = true,
            keyProperty = "e.id"
    )
    @Insert("insert into goods_info " +
            "(goods_name,market_price,express_type,postage,goods_no,upload_time,brand_label,del)" +
            "values(#{e.goodsName},#{e.marketPrice},#{e.expressType},#{e.postage},#{e.goodsNo},now(),'1','0')")
    int saveGoodsBeanEntity(@Param("e") GoodsBean goodsBean);

    /**
     * 修改非实物
     *
     * @param goodsBean
     * @return
     */
    @Update("<script>" +
            "update goods_info" +
            "<set>" +
            "<if test='g.goodsName != null'>" +
            " goods_name = #{g.goodsName}," +
            "</if>" +
            "<if test='g.goodsDesc != null'>" +
            " goods_desc = #{g.goodsDesc}," +
            "</if>" +
            "<if test='g.brandLabel != null'>" +
            " brand_label = #{g.brandLabel}," +
            "</if>" +
            "<if test='g.classHour != null'>" +
            " class_hour = #{g.classHour}," +
            "</if>" +
            "<if test='g.image != null'>" +
            " image = #{g.image}," +
            "</if>" +
            "<if test='g.fkProjectId != null'>" +
            " fk_teacher_id = #{g.fkProjectId}," +
            "</if>" +
            "<if test='g.fkProjectId != null'>" +
            " fk_project_id = #{g.fkProjectId}," +
            "</if>" +
            "<if test='g.useful != null'>" +
            " useful = #{g.useful}," +
            "</if>" +
//            "<if test='g.detail != null'>" +
//            " detail = #{g.detail}," +
//            "</if>" +
            "<if test='g.marketPrice != null'>" +
            " market_price = #{g.marketPrice}," +
            "</if>" +
            "<if test='g.salesPrice != null'>" +
            " sales_price = #{g.salesPrice}," +
            "</if>" +
            "<if test='g.stock != null'>" +
            " stock = #{g.stock}," +
            "</if>" +
            "<if test='g.status != null'>" +
            " status = #{g.status}," +
            "</if>" +
            "</set>" +
            "where id = #{g.id} and brand_label= 0" +
            "</script>")
    int updateGoodsBean(@Param("g") GoodsBean goodsBean);


    /**
     * 修改实物商品
     *
     * @param goodsBean
     * @return
     */
    @Update("<script>" +
            "update goods_info" +
            "<set>" +
            "<if test='g.goodsName != null'>" +
            " goods_name = #{g.goodsName}," +
            "</if>" +
            "<if test='g.marketPrice != null'>" +
            " market_price = #{g.marketPrice}," +
            "</if>" +
            "<if test='g.expressType != null'>" +
            " express_type = #{g.expressType}," +
            "</if>" +
            "<if test='g.postage != null'>" +
            " postage = #{g.postage}," +
            "</if>" +
            "</set>" +
            "where id = #{g.id} and brand_label= 1" +
            "</script>")
    int updateEntity(@Param("g") GoodsBean goodsBean);

    /**
     * 推荐设置
     * 0--推荐
     * 1--不推荐
     *
     * @param goodsBean
     * @return
     */
    @Update("update goods_info set recommend = #{recommend} where id=#{id} ")
    int updateRecommend(GoodsBean goodsBean);

    /**
     * 下架
     *
     * @param goodsBean
     * @return
     */
    @Update("update goods_info set status=#{status} where id=#{id}")
    int updateStatus(GoodsBean goodsBean);

    /**
     * 模糊查询
     *
     * @param goodsBean
     * @return
     */
    List<GoodsBean> findAllByParams(@Param("g") GoodsBean goodsBean);

    /**
     *
     * @param goodsBean
     * @return
     */
    List<GoodsBean> findEntityParams(@Param("g") GoodsBean goodsBean);

    /**
     * 查询所有的实物
     * @return
     */
    @Select("select * from goods_info where del=0 and brand_label=1 ")
    List<GoodsBean> findEntity();


}
